﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcPaging;
using QuocHoi_TV_Frontend.Models;

namespace QuocHoi_TV_Frontend.Controllers
{
    public class BroadcastController : Controller
    {
        //
        // GET: /Broadcast/

        public ActionResult Index()
        {
            ViewBag.lstCate = DataModels.getCateHaveVideo(10);
            List<News> lstVideoNew = DataModels.GetVideoNews(6);
            ViewBag.lstVideoNew = lstVideoNew;
            return View();
        }

        public ActionResult Detail(long cateID=0, long newsID = 0, int page = 1)
        {            
            if (newsID > 0)
            {
                News oneVideo = DataModels.SelectNewsOne(newsID);
                ViewBag.video = oneVideo;
                cateID = oneVideo.CateId.Value;
                List<News> videoRelate = DataModels.GetNewsRelate(newsID, cateID, 5);
                ViewBag.lstVideoNew = videoRelate;  
            }           
            if (newsID == 0)
            {
                
                List<News> lstVideoNew = DataModels.GetNewsByCate(cateID, 6);
                ViewBag.video = lstVideoNew[0];
                ViewBag.lstVideoNew = lstVideoNew; 
            }
            ViewBag.lstCate = DataModels.getCateHaveVideoByCateId(cateID, 10);
            int currentPageIndex = page;

            List<News> video = DataModels.GetNewsByCate(cateID, 100);

            //var list = 
            if (Request.IsAjaxRequest())
                return PartialView("_ListVideo", video.ToPagedList(currentPageIndex, 8));
            else
                return View(video.ToPagedList(currentPageIndex, 8)); 
        }

        public ActionResult VideoDetail(long newsID, int page = 1)
        {
            News oneVideo = DataModels.SelectNewsOne(newsID);
            ViewBag.video = oneVideo;
            List<CommentNew> comment = DataModels.getCommentByNewsID(newsID);
            ViewBag.countComment = comment.Count();
            int currentPageIndex = page;
            if (Request.IsAjaxRequest())
                return PartialView("_ListComments", comment.ToPagedList(currentPageIndex, 8));
            else
                return View(comment.ToPagedList(currentPageIndex, 8)); 
        }

        [HttpPost]
        public ActionResult GetVideo(long newsID)
        {
            News oneVideo = DataModels.SelectNewsOne(newsID);
            return PartialView("_VideoDetail", oneVideo);
        }

        public ActionResult GetVideoRelate(long newsID, long cateID)
        {
            List<News> video = DataModels.GetNewsRelate(newsID, cateID, 6);
            return PartialView();
        }

        [HttpPost]
        public ActionResult PostCommnet(long newsID, string name, string email, string content)
        {
            DataModels.PostComment(newsID, name, email, content);
            List<CommentNew> comment = DataModels.getCommentByNewsID(newsID);
            return PartialView("_ListComments", comment);
        }
    }
}
